<?php namespace Admin\Controller;
use Hdphp\Controller\Controller;

class CategoryController extends CommonController{
	private $model;
	public function __construct(){
		parent::__construct();
		$this->model = new \Common\Model\Category;
	}
	public function index(){
//		// 分页
//		$total = $this->model->count();
//		View::with('total',$total);
//		// 执行分页
//		$page = Page::row(10)->make($total);
//		View::with('page',$page);
//		limit(Page::limit())->

		$cateData = $this->model->get();
		$cateData = Data::tree($cateData,'cname');
		View::with('cateData',$cateData);
		View::make();
	}

	public function add(){
		if(IS_POST){
			if($this->model->addData(Q('post.'))){
				View::success('添加成功',U('index'));
			}
			View::error($this->model->getError());
		}
		$data = Db::table('type')->get();
		View::with('data',$data);
		View::make();
	}

	public function edit(){
		if(IS_POST){
			if($this->model->editData()){
				View::success('修改完成',U('index'));
			}
			View::error($this->model->getError());
		}
		$cid = Q('get.cid','0','intval');
		$oldData = Db::table('type')->join('category','tid','=','type_tid')->where("cid={$cid}")->get();
		View::with('oldData',$oldData[0]);
		$cate = $this->model->getNoMy($cid);
		$cate = Data::tree($cate,'cname');
		View::with('cate',$cate);
		View::make();
	}

	public function del(){
		$this->model->delData(Q('get.cid','0','intval'));
		View::success('删除成功',U('index'));
	}

	public function addSon(){
		if(IS_POST){
			if($this->model->addSonData()){
				View::success('添加成功',U('index'));
			}
			View::error($this->model->getError());
		}
		$cid = Q('get.cid','0','intval');
		$data = Db::table('type')->join('category','tid','=','type_tid')->where("cid={$cid}")->get();
		View::with('data',$data[0]);
		View::make();
	}
}



 ?>